//
//  ScenicStorySpeedView.swift
//  LeTu
//
//  Created by g1 on 2024/2/4.
//

import UIKit

class ScenicStorySpeedView: UIView {

    private let speeds = ["0.75", "0.85", "1.0", "1.5", "2.0"]
    
    init() {
        super.init(frame: .zero)
        
        configUI()
    }
    
    // 获取view宽度
    func getSpeedViewWidth() -> CGFloat {
        var speedViewWidth = 0.0
        for text in speeds {
            speedViewWidth += "\(text)x".widthWithLimit( CGSize(width: CGFloat.greatestFiniteMagnitude, height: 18), font: kSetPingFangLight(14)) + 16
        }
        return speedViewWidth
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(speedsCollectionView)
        
        speedsCollectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        speedsCollectionView.delegate = self
        speedsCollectionView.dataSource = self
        speedsCollectionView.register(cellClass: ScenicStorySpeedCollectionViewCell.self)
        
    }
    private lazy var speedsCollectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        let speedsCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        speedsCollectionView.backgroundColor = UIColor.white.withAlphaComponent(0.3)
        speedsCollectionView.layer.cornerRadius = 6
        speedsCollectionView.layer.masksToBounds = true
        return speedsCollectionView
    }()

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ScenicStorySpeedView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let speedText = speeds[indexPath.item] + "x"
        return CGSize(width: speedText.widthWithLimit( CGSize(width: CGFloat.greatestFiniteMagnitude, height: 18), font: kSetPingFangLight(14)) + 16, height: collectionView.height)
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return speeds.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(ScenicStorySpeedCollectionViewCell.self, indexPath: indexPath)
        let speed = speeds[indexPath.item]
        cell.setSpeed(speed: speed + "x", isSelect: Float(speed) == AppAudioManager.shared.rate)
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let speed = speeds[indexPath.item]
        AppAudioManager.shared.rate = Float(speed) ?? 1.0
        collectionView.reloadData()
    }
    
}

class ScenicStorySpeedCollectionViewCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    func setSpeed(speed: String, isSelect: Bool) {
        speedsLabel.text = speed
        speedsLabel.textColor = isSelect ? .app_themeColor:UIColor.white
    }
    
    // MARK: - configUI
    func configUI() {
        
        contentView.addSubview(speedsLabel)
        
        speedsLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
    }
    private lazy var speedsLabel: UILabel = {
        let speedsLabel = UILabel()
        speedsLabel.font = kSetPingFangLight(14)
        speedsLabel.textColor = UIColor.white
        return speedsLabel
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

